"""
分析结果相关的Pydantic模型定义
"""

from datetime import datetime
from typing import List, Optional, Dict, Any, Literal
from pydantic import BaseModel, Field


class TechnicalAnalysis(BaseModel):
    """技术分析结果模型"""
    signal: Literal["bullish", "bearish", "neutral"] = Field(..., description="技术分析信号")
    confidence: float = Field(..., ge=0, le=1, description="置信度")
    reasoning: Dict[str, Any] = Field(..., description="分析推理过程")
    strategy_signals: Literal["bullish", "bearish", "neutral"] = Field(..., description="策略信号")
    strategy_reasoning: Dict[str, Any] = Field(..., description="策略推理过程")
    strategy_confidence: float = Field(..., ge=0, le=1, description="策略置信度")
    support_resistance: Optional[Dict[str, float]] = Field(None, description="支撑阻力位")


class FundamentalAnalysis(BaseModel):
    """基本面分析结果模型"""
    signal: Literal["bullish", "bearish", "neutral"] = Field(..., description="基本面分析信号")
    confidence: float = Field(..., ge=0, le=1, description="置信度")
    reasoning: Dict[str, Any] = Field(..., description="分析推理过程")
    profitability_score: Optional[float] = Field(None, description="盈利能力评分")
    growth_score: Optional[float] = Field(None, description="成长性评分")
    financial_health_score: Optional[float] = Field(None, description="财务健康评分")
    valuation_score: Optional[float] = Field(None, description="估值评分")
    key_metrics: Dict[str, float] = Field(default_factory=dict, description="关键财务指标")


class SentimentAnalysis(BaseModel):
    """情绪分析结果模型"""
    signal: Literal["bullish", "bearish", "neutral"] = Field(..., description="情绪分析信号")
    confidence: float = Field(..., ge=0, le=1, description="置信度")
    reasoning: str = Field(..., description="分析推理过程")
    sentiment_score: float = Field(..., ge=-1, le=1, description="情绪分数")
    news_count: int = Field(..., description="分析的新闻数量")
    positive_news_ratio: Optional[float] = Field(None, description="正面新闻比例")
    key_themes: List[str] = Field(default_factory=list, description="关键主题")
    market_mood: Optional[str] = Field(None, description="市场情绪描述")


class ValuationAnalysis(BaseModel):
    """估值分析结果模型"""
    signal: Literal["bullish", "bearish", "neutral"] = Field(..., description="估值分析信号")
    confidence: float = Field(..., ge=0, le=1, description="置信度")
    reasoning: Dict[str, Any] = Field(..., description="分析推理过程")
    intrinsic_value: Optional[float] = Field(None, description="内在价值")
    current_price: Optional[float] = Field(None, description="当前价格")
    upside_potential: Optional[float] = Field(None, description="上涨潜力")
    valuation_ratios: Dict[str, float] = Field(default_factory=dict, description="估值比率")
    dcf_value: Optional[float] = Field(None, description="DCF估值")
    relative_valuation: Optional[str] = Field(None, description="相对估值评价")


class ResearcherAnalysis(BaseModel):
    """研究员分析结果模型"""
    perspective: Literal["bull", "bear"] = Field(..., description="研究员观点")
    confidence: float = Field(..., ge=0, le=1, description="置信度")
    thesis_points: List[str] = Field(..., description="论点列表")
    supporting_evidence: List[str] = Field(default_factory=list, description="支持证据")
    risk_factors: List[str] = Field(default_factory=list, description="风险因素")
    price_target: Optional[float] = Field(None, description="目标价格")
    time_horizon: Optional[str] = Field(None, description="投资时间框架")
    conviction_level: Optional[str] = Field(None, description="确信程度")


class DebateResult(BaseModel):
    """辩论结果模型"""
    signal: Literal["bullish", "bearish", "neutral"] = Field(..., description="辩论结果信号")
    confidence: float = Field(..., ge=0, le=1, description="最终置信度")
    bull_confidence: float = Field(..., ge=0, le=1, description="多方置信度")
    bear_confidence: float = Field(..., ge=0, le=1, description="空方置信度")
    confidence_diff: float = Field(..., description="置信度差异")
    llm_score: Optional[float] = Field(None, description="LLM第三方评分")
    llm_analysis: Optional[str] = Field(None, description="LLM分析内容")
    llm_reasoning: Optional[str] = Field(None, description="LLM推理过程")
    mixed_confidence_diff: float = Field(..., description="混合置信度差异")
    debate_summary: List[str] = Field(..., description="辩论摘要")
    reasoning: str = Field(..., description="最终推理")
    key_disagreements: List[str] = Field(default_factory=list, description="主要分歧点")
    consensus_points: List[str] = Field(default_factory=list, description="共识点")


class RiskAssessment(BaseModel):
    """风险评估结果模型"""
    signal: Literal["buy", "sell", "hold"] = Field(..., description="风险管理信号")
    confidence: float = Field(..., ge=0, le=1, description="置信度")
    max_position_size: int = Field(..., description="最大持仓数量")
    risk_score: float = Field(..., ge=0, le=1, description="风险评分")
    volatility: Optional[float] = Field(None, description="波动率")
    beta: Optional[float] = Field(None, description="Beta系数")
    var_95: Optional[float] = Field(None, description="95% VaR")
    max_drawdown: Optional[float] = Field(None, description="最大回撤")
    sharpe_ratio: Optional[float] = Field(None, description="夏普比率")
    risk_factors: List[str] = Field(default_factory=list, description="风险因素")
    risk_mitigation: List[str] = Field(default_factory=list, description="风险缓解措施")
    stop_loss: Optional[float] = Field(None, description="止损价格")
    take_profit: Optional[float] = Field(None, description="止盈价格")


class MacroAnalysis(BaseModel):
    """宏观分析结果模型"""
    signal: Literal["bullish", "bearish", "neutral"] = Field(..., description="宏观分析信号")
    confidence: float = Field(..., ge=0, le=1, description="置信度")
    reasoning: str = Field(..., description="分析推理过程")
    economic_indicators: Dict[str, float] = Field(default_factory=dict, description="经济指标")
    policy_impact: Optional[str] = Field(None, description="政策影响分析")
    market_environment: Optional[str] = Field(None, description="市场环境评估")
    sector_outlook: Optional[str] = Field(None, description="行业前景")
    global_factors: List[str] = Field(default_factory=list, description="全球因素")
    domestic_factors: List[str] = Field(default_factory=list, description="国内因素")


class AgentExecutionLog(BaseModel):
    """智能体执行日志模型"""
    agent_name: str = Field(..., description="智能体名称")
    execution_time: datetime = Field(..., description="执行时间")
    input_data: Dict[str, Any] = Field(..., description="输入数据")
    output_data: Dict[str, Any] = Field(..., description="输出数据")
    execution_duration: float = Field(..., description="执行时长（秒）")
    status: Literal["success", "error", "warning"] = Field(..., description="执行状态")
    error_message: Optional[str] = Field(None, description="错误信息")
    reasoning: Optional[Dict[str, Any]] = Field(None, description="推理过程")
    llm_interactions: List[str] = Field(default_factory=list, description="LLM交互记录")


class WorkflowSummary(BaseModel):
    """工作流摘要模型"""
    run_id: str = Field(..., description="运行ID")
    ticker: str = Field(..., description="股票代码")
    start_time: datetime = Field(..., description="开始时间")
    end_time: Optional[datetime] = Field(None, description="结束时间")
    total_duration: Optional[float] = Field(None, description="总执行时长（秒）")
    status: Literal["running", "completed", "failed"] = Field(..., description="工作流状态")
    agents_executed: List[str] = Field(default_factory=list, description="已执行的智能体")
    final_decision: Optional[Dict[str, Any]] = Field(None, description="最终决策")
    performance_metrics: Dict[str, float] = Field(default_factory=dict, description="性能指标")
    error_count: int = Field(0, description="错误数量")
    warning_count: int = Field(0, description="警告数量")
